/*
 * $Id: ModelTypePropDAO.java,v 1.5 2009/07/10 15:18:36 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.ge.healthcare.autosc.dao.intf.IModelTypePropDAO;
import com.ge.healthcare.autosc.model.CfgModelTypeProp;
import com.ge.healthcare.autosc.model.CfgModelTypePropId;

/**
 * @author 212042764
 * 
 */
public class ModelTypePropDAO  implements IModelTypePropDAO {
	/**
	 * Eclispe generated
	 */
	private static final long serialVersionUID = -1487748325109950260L;
	private static final String ANY_VALUE = "*";
	private static final String TABLE_NAME = "com.ge.healthcare.autosc.common.database.pojo.CfgModelTypeProp";
	private static final String COL_MODALITY = "id.modality";
	private static final String COL_PRODUCT_LINE = "id.productLine";
	private static final String COL_MODEL_TYPE = "id.modelType";
	private static final String COL_PROPERTY_NAME = "id.propertyName";
	

	@Autowired
	private HibernateTemplate hibernateTemplate;

	public ModelTypePropDAO() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.ge.healthcare.autosc.common.database.dao.IModelTypePropDAO#
	 * getValueForProperty(java.lang.String, java.lang.String, java.lang.String)
	 */
	public String getValueForProperty(String modality, String productLine,
			String modelType, String propertyName) {
		String value = this.getValue(modality, productLine, modelType,
				propertyName);
		if (value == null || value.isEmpty()) {
			value = this.getValueForPropertyWithoutModelType(modality,
					productLine, propertyName);
		}
		if (value == null || value.isEmpty()) {
			value = this.getValueForPropertyWithOnlyModality(modality,
					propertyName);
		}
		return value;
	}

	private String getValueForPropertyWithoutModelType(String modality,
			String productLine, String propertyName) {
		return this.getValue(modality, productLine, ANY_VALUE, propertyName);
	}

	private String getValueForPropertyWithOnlyModality(String modality,
			String propertyName) {
		return this.getValue(modality, ANY_VALUE, ANY_VALUE, propertyName);
	}

	private String getValue(String modality, String productLine,
			String modelType, String propertyName) {
		Map<String, Object> whereClause = new HashMap<String, Object>();
		whereClause.put(COL_MODALITY, modality);
		whereClause.put(COL_PRODUCT_LINE, productLine);
		whereClause.put(COL_MODEL_TYPE, modelType);
		whereClause.put(COL_PROPERTY_NAME, propertyName);
		List<?> propLst = this.findByProperties(whereClause);
		if (propLst== null || propLst.isEmpty()) {
			return null;
		}
		return ((CfgModelTypeProp) propLst.get(0)).getPropertyValue();
	}
	
	public List<?> findByProperties(Map<String, Object> whereClause) {
		if (whereClause == null || whereClause.isEmpty()) {
			return null;
		}
		StringBuffer queryString = new StringBuffer().append("from ").append(
				TABLE_NAME).append(" where ");
		Iterator<String> properties = whereClause.keySet().iterator();
		boolean firstRun = true;
		while (properties.hasNext()) {
			if (firstRun) {
				firstRun = false;
			} else {
				queryString.append(" and ");
			}
			String propertyName = properties.next();
			queryString.append(propertyName).append("= ? ");
		}
		Object[] values = whereClause.values().toArray();
		return hibernateTemplate.find(queryString.toString(), values);
	}

	public void saveAlways(Object pojo) {
		hibernateTemplate.save(pojo);

	}

	@Override
	public void save(Object pojo) {
		// TODO Auto-generated method stub
		hibernateTemplate.saveOrUpdate(pojo);
	}

	@Override
	public void deleteByID(Object pojo) {
		// TODO Auto-generated method stub
		hibernateTemplate.delete(pojo);
	}

	@Override
	public void refresh(Object pojo) {
		// TODO Auto-generated method stub
		hibernateTemplate.refresh(pojo);
	}
	
	@Override
	public List<String> getAllModalities() {
		DetachedCriteria dc = DetachedCriteria.forClass(getDataClass(),"prop");
		dc.setProjection(Projections.property("prop.id.modality"));
		List<String> CfgModelTypePropList = new ArrayList<String>();
		try {
			CfgModelTypePropList = (List<String>)hibernateTemplate.findByCriteria(dc);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return CfgModelTypePropList;
	}
	
	protected Class<CfgModelTypeProp> getDataClass() {
		return CfgModelTypeProp.class;
	}

	@Override
	public List<String> getProductLinesByModality(String modality) {
		DetachedCriteria dc = DetachedCriteria.forClass(getDataClass(),"prop");
		dc.setProjection(Projections.property("prop.id.productLine"));
		List<String> cfgModelTypePropIdList = new ArrayList<String>();
		if(StringUtils.isNotBlank(modality)){
			dc.add(Restrictions.eq("id.modality", modality.trim()));
		}
		
		try {
			cfgModelTypePropIdList = (List<String>)hibernateTemplate.findByCriteria(dc);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cfgModelTypePropIdList;
	}

	@Override
	public List<String> getModelTypes(String modality,String productLine) {
		DetachedCriteria dc = DetachedCriteria.forClass(getDataClass(),"prop");
		dc.setProjection(Projections.property("prop.id.modelType"));
		List<String> cfgModelTypePropIdList = new ArrayList<String>();
		if(StringUtils.isNotBlank(modality)){
			dc.add(Restrictions.eq("id.modality", modality.trim()));
		}
		if(StringUtils.isNotBlank(productLine)){
			dc.add(Restrictions.eq("id.productLine", productLine.trim()));
		}
		try {
			cfgModelTypePropIdList = (List<String>)hibernateTemplate.findByCriteria(dc);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cfgModelTypePropIdList;
	}

}
